/*
 * @Author: chendeji
 * @Date: 2021-11-18 12:47:39
 * @LastEditTime: 2021-11-18 13:10:01
 * @Description: 
 * @FilePath: \copy_gsy_github_app_flutter\lib\page\main\widgets\event_widget.dart
 * 联系方式：
 * wx: melody_2009
 * qq: 7815171323 
 */

import 'package:copy_gsy_github_app_flutter/common/model/Event.dart';
import 'package:copy_gsy_github_app_flutter/common/model/RepoCommit.dart';
import 'package:copy_gsy_github_app_flutter/common/style/gsy_style.dart';
import 'package:copy_gsy_github_app_flutter/utils/common_utils.dart';
import 'package:copy_gsy_github_app_flutter/utils/event_utils.dart';
import 'package:copy_gsy_github_app_flutter/common/model/Notification.dart'
    as Model;
import 'package:copy_gsy_github_app_flutter/widget/card_item.dart';
import 'package:copy_gsy_github_app_flutter/widget/user_icon_widget.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class EventItem extends StatelessWidget {
  final EventViewModel eventViewModel;

  final VoidCallback? onPressed;

  final bool needImage;
  const EventItem(this.eventViewModel,
      {Key? key, this.onPressed, this.needImage = true})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget des = (eventViewModel.actionDes == null ||
            eventViewModel.actionDes!.length == 0)
        ? new Container()
        : new Container(
            child: new Text(
              eventViewModel.actionDes!,
              style: APPFont.smallSubText,
              maxLines: 3,
            ),
            margin: new EdgeInsets.only(top: 6.0.w, bottom: 2.0.h),
            alignment: Alignment.topLeft);

    Widget userImage = (needImage)
        ? UserIconWidget(
            padding: EdgeInsets.only(top: 0.0.h, right: 5.0.w, left: 0.0.w),
            width: 30.0.w,
            height: 30.0.h,
            image: eventViewModel.actionUserPic,
            onPressed: () {
              /// TODO 跳转到用户详情界面
              ///
              // NavigatorUtils.goPerson(context, eventViewModel.actionUser);
            })
        : Container();
    return Container(
      child: CardItem(
          child: TextButton(
              onPressed: onPressed,
              child: Padding(
                padding: EdgeInsets.only(
                    left: 0.0.w, top: 10.0.h, right: 0.0.w, bottom: 10.0.h),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        userImage,
                        Expanded(
                            child: Text(eventViewModel.actionUser!,
                                style: APPFont.smallTextBold)),
                        Text(eventViewModel.actionTime,
                            style: APPFont.smallSubText),
                      ],
                    ),
                    Container(
                        child: Text(eventViewModel.actionTarget!,
                            style: APPFont.smallTextBold),
                        margin: EdgeInsets.only(top: 6.0.w, bottom: 2.0.h),
                        alignment: Alignment.topLeft),
                    des,
                  ],
                ),
              ))),
    );
  }
}

class EventViewModel {
  String? actionUser;
  String? actionUserPic;
  String? actionDes;
  late String actionTime;
  String? actionTarget;

  EventViewModel.fromEventMap(Event event) {
    actionTime = CommonUtils.getNewsTimeStr(event.createdAt!);
    actionUser = event.actor!.login;
    actionUserPic = event.actor!.avatar_url;
    var other = EventUtils.getActionAndDes(event);
    actionDes = other["des"];
    actionTarget = other["actionStr"];
  }

  EventViewModel.fromCommitMap(RepoCommit eventMap) {
    actionTime = CommonUtils.getNewsTimeStr(eventMap.commit!.committer!.date!);
    actionUser = eventMap.commit!.committer!.name;
    actionDes = "sha:" + eventMap.sha!;
    actionTarget = eventMap.commit!.message;
  }

  EventViewModel.fromNotify(BuildContext context, Model.Notification eventMap) {
    actionTime = CommonUtils.getNewsTimeStr(eventMap.updateAt!);
    actionUser = eventMap.repository!.fullName;
    String? type = eventMap.subject!.type;
    String status = eventMap.unread! ? 'notify_unread'.tr : 'notify_readed'.tr;
    actionDes = eventMap.reason! +
        "${'notify_type'.tr}：$type，${'notify_status'.tr}：$status";
    actionTarget = eventMap.subject!.title;
  }
}
